package draw;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class JsonToImage {
    public static void main(String[] args) {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Data");
        Object[] headers = {"ID", "Name", "Age"};
        Object[][] data = {
                {1, "Alice", 25},
                {2, "Bob", 30},
                {3, "Charlie", 35}
        };

        Row headerRow = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i].toString());
        }

        for (int i = 0; i < data.length; i++) {
            Row row = sheet.createRow(i + 1);
            for (int j = 0; j < data[i].length; j++) {
                Cell cell = row.createCell(j);
                cell.setCellValue(data[i][j].toString());
            }
        }


        ByteArrayOutputStream out = new ByteArrayOutputStream();
        workbook.write(out);
        workbook.close();

        byte[] fileData = out.toByteArray();

        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));

        ImageRenderer imageRenderer = new ImageRenderer(new Image(ImageDataFactory.create(fileData)));
        imageRenderer.draw(pdfDoc, new Rectangle(0, 0, imageRenderer.getImageWidth(), imageRenderer.getImageHeight()));

        pdfDoc.close();
    }
}
